package com.sxj.corejava.code09_集合;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author 石小俊
 */
public class Test_Collections工具类 {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        // 参数一:向哪个集合添加数据
        // 参数二:可变长参数,添加哪些数据
        Collections.addAll(list, 11, 21, 1, 5, 7, 18, 10, 2);
        System.out.println(list);

        List<String> names = new ArrayList<>();
        Collections.addAll(names, "admin", "tom", "lucy", "jack", "alice", "john", "alex");
        System.out.println(names);

        System.out.println("list集合中最大值:" + Collections.max(list));
        System.out.println("list集合中最小值:" + Collections.min(list));
        System.out.println("names集合中最大值:" + Collections.max(names));
        System.out.println("names集合中最小值:" + Collections.min(names));

        // 排序,默认是升序排序
        Collections.sort(list);
        Collections.sort(names);
        System.out.println(list);
        System.out.println(names);

        // 自定义排序规则,降序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1 > o2) {
                    return -1;
                } else if (o1 < o2) {
                    return 1;
                }
                return 0;
            }
        });
        System.out.println(list);

        // 按照姓名长度降序
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() > o2.length()) {
                    return -1;
                } else if (o1.length() < o2.length()) {
                    return 1;
                }
                return 0;
            }
        });
        System.out.println(names);
        Collections.reverse(names);
        System.out.println(names);
        Collections.replaceAll(list, 1, 0);
        System.out.println(list);
        Collections.swap(list, 1, 6);
        System.out.println(list);

        Collections.fill(list, 0);
        System.out.println(list);
    }

}
